#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

template <typename  Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

class Point
{
public:
    Point(int ix = 0, int iy = 0)
    : _ix(ix)
    , _iy(iy)
    {
        cout << "Point(int = 0, int = 0)" << endl;
    }

    Point(const Point &rhs)
    : _ix(rhs._ix)
    , _iy(rhs._iy)
    {
        cout << "Point(const Point &)" << endl;
    }
private:
    int _ix;
    int _iy;
};

void test00()
{
    vector<Point> vec;
    /* vec.push_back(Point(1, 2)); */
    vec.emplace_back(1, 2);
}

void test()
{
    cout << "sizeof(vector<int>) = " << sizeof(vector<int>) << endl;
    cout << "sizeof(vector<long>) = " << sizeof(vector<long>) << endl;
    vector<int> number = {1, 3, 5, 7, 9, 8, 6, 4, 2, 10};
    display(number);
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;

    cout << endl << "在vector的尾部进行插入与删除" << endl;
    number.push_back(100);
    number.push_back(200);
    display(number);
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;
    number.pop_back();
    display(number);
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;

    //没有在vector的头部提供插入与删除的方法？
    //因为vector是一端开口的，如果在头部插入一个元素，那么
    //后面的元素都会后移一个存放新插入的元素；如果在头部
    //删除一个元素，后面的元素都会向前移动一个，那么时间
    //复杂度比较高O(N)

    cout << endl << "在vector的任意位置插入元素" << endl;
    auto it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, 300);
    display(number);
    cout << "*it = " << *it << endl;
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;

    cout << endl;
    //vector中使用insert进行插入操作的时候，因为insert插入元素的个数
    //是不确定的，有可能导致底层已经发生了扩容，而迭代器还指向老的
    //空间，那么下一次再使用该迭代器进行插入的时候，就有可能发生问题
    //就将上述问题成为迭代器失效
    //解决方案：每次使用迭代器之前，都将迭代器重新置位
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, 10, 55);
    display(number);
    cout << "*it = " << *it << endl;
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;

    cout << endl;
    vector<int> vec = {12, 34, 56, 78};
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, vec.begin(), vec.end());
    display(number);
    cout << "*it = " << *it << endl;
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;

    cout << endl;
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, {666, 888, 999});
    display(number);
    cout << "*it = " << *it << endl;
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;

    cout << endl << "清空vector中的元素" << endl;
    number.clear();
    number.shrink_to_fit();
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;
}

int main(int argc, char *argv[])
{
    test00();
    /* vector<int> vec; */
    /* vec[0] = 100; */
    return 0;
}

